package com.inspur.gs.sbssdl.sbssgl.sbtz.sbtz.back.sbsssbtz.determinations;

import com.inspur.edp.bef.api.be.IBENodeEntityContext;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bef.engine.entity.AssociationInfo;
import com.inspur.edp.cef.api.message.*;
import com.inspur.edp.bef.api.action.determination.*;
import com.inspur.edp.bef.spi.action.determination.*;
import com.inspur.edp.cef.entity.changeset.*;
import com.inspur.edp.bef.spi.action.determination.AbstractDetermination;
import com.inspur.edp.bef.api.action.determination.IDeterminationContext;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.bef.api.action.determination.IBeforeRetrieveDtmContext;
import com.inspur.edp.cef.entity.entity.IValuesContainer;
import com.inspur.edp.cef.variable.api.data.IVariableData;
import com.inspur.gs.sbssdl.sbssgl.sbtz.sbtz.back.sbsssbtz.common.SbssSbtzLogUtils;
import io.iec.edp.caf.boot.context.CAFContext;

import java.util.ArrayList;

public class SbssSbtzValueChangedDtms1MgrAction extends AbstractB4SaveDetermination {

    private String SbtzId = "efd56b39-0e8d-4441-9fa1-2b019b6fd862";

    public SbssSbtzValueChangedDtms1MgrAction(IDeterminationContext context, IChangeDetail change) {
        super(context, change);
    }

    @Override
    public void execute() {
        // 原始
        IEntityData org = getContext().getOriginalData();
        // 修改
        IEntityData cur = getContext().getCurrentData();

        String oldVal = "";
        String updVal = "";

        if (org == null || cur == null) {
            return;
        }

        try {
            Object oldBmbh = org.getValue("bmbh_id");
            if (oldBmbh != null) {
                oldVal = ((IValuesContainer) org.getValue("bmbh_id")).getValue("bmbh_id_name").toString();
            }
        } catch (Exception ignored) {
        }

        try {
            Object updBmbh = cur.getValue("bmbh_id");
            if (updBmbh != null) {
                updVal = ((IValuesContainer) cur.getValue("bmbh_id")).getValue("bmbh_id_name").toString();
            }
        } catch (Exception ignored) {
        }
        if (oldVal.equals(updVal)) {
            return;
        }

        // 获取全局变量，否则设定默认值
        IBENodeEntityContext beContext = getContext().getBEContext();
        IVariableData variables = beContext.getVariables();
        String DataName = isNotNull(variables.getValue("DataName")) ?  variables.getValue("DataName").toString() : "bmbh_id";
        String DataComm = isNotNull(variables.getValue("DataComm")) ?  variables.getValue("DataComm").toString() : "部门" ;
        String oldValue = isNotNull(variables.getValue("oldValue")) ?  variables.getValue("oldValue").toString() : oldVal ;
        String updValue = isNotNull(variables.getValue("updValue")) ?  variables.getValue("updValue").toString() : updVal ;
        String valueFrom = isNotNull(variables.getValue("valueFrom")) ? variables.getValue("valueFrom").toString() : "台账修改";

        String userId = CAFContext.current.getCurrentSession().getUserId();
        String changeUser = variables.getValue("changeUser") == null ? userId : variables.getValue("changeUser").toString();

        SbssSbtzLogUtils.changelog(org.getID(), DataName, DataComm, oldValue, updValue, valueFrom, changeUser);

    }

    private IEntityData getData() {
        return (IEntityData) super.getContext().getCurrentData();
    }

    private boolean isNotNull(Object obj) {
        return obj != null && !obj.toString().isEmpty();
    }
}